package org.wk.news.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.wk.core.common.view.PageVo;
import org.wk.core.common.view.ResponseVo;
import org.wk.news.dao.NewsClassifiedsDao;
import org.wk.news.domain.NewsClassifieds;
import org.wk.news.domain.NewsManage;
import org.wk.news.view.NewsClassifiedsVo;
import org.wk.news.view.NewsManageVo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName NewsClassifiedsServiceImpl
 * @Description
 * @Author ZhaoPing'an
 * @Date 2018/6/5 14:17
 */
@Service
public class NewsClassifiedsServiceImpl implements NewsClassifiedsService{

    @Autowired
    private NewsClassifiedsDao typeDao;

    /**
     * @Description 资讯类型列表页面
     * @Param [params]
     * @author ZhaoPing'an
     * @Date 2018/6/5 11:18
     */
    @Override
    public PageVo findClassifiedsListPage(Map<String, Object> params) {
        Integer pageNo = 0;
        Integer pageSize = 10;
        Page<NewsClassifieds> page = typeDao.findAll(PageRequest.of(pageNo, pageSize));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        List<NewsClassifiedsVo> result = new ArrayList<>();
        for (NewsClassifieds temp: page.getContent() ) {
            NewsClassifiedsVo classifiedsVo = new NewsClassifiedsVo();
            BeanUtils.copyProperties(temp,classifiedsVo);
            classifiedsVo.setCreateTime(sdf.format(temp.getCreateTime()));
            result.add(classifiedsVo);
        }
        PageVo pageVo = new PageVo<NewsClassifiedsVo>();
        pageVo.setTotal(page.getTotalElements());
        pageVo.setPageNo(++pageNo);
        pageVo.setPageSize(pageSize);
        pageVo.setResult(result);
        pageVo.setTotalPages(page.getTotalPages());
        return pageVo;
    }

    /**
     * @Description 保存资讯类型
     * @Param [newsType]
     * @author ZhaoPing'an
     * @Date 2018/6/5 11:45
     */
    @Override
    public void insert(NewsClassifiedsVo newsClassifiedsVo) {
        NewsClassifieds entity = new NewsClassifieds();
        BeanUtils.copyProperties(newsClassifiedsVo,entity);
        typeDao.save(entity);
    }

    /**
     * @Description 获取资讯分类详细信息
     * @Param [id]
     * @author ZhaoPing'an
     * @Date 2018/6/5 15:49
     */
    @Override
    public NewsClassifiedsVo findNewsClassifiedsInfo(String id) {
        NewsClassifiedsVo newsClassifiedsVo = new NewsClassifiedsVo();
        NewsClassifieds result = typeDao.getOne(id);
        BeanUtils.copyProperties(result,newsClassifiedsVo);
        return newsClassifiedsVo;
    }

    /**
     * @Description 修改
     * @Param [newsClassifiedsVo]
     * @author ZhaoPing'an
     * @Date 2018/6/5 16:14
     */
    @Override
    public void update(NewsClassifiedsVo newsClassifiedsVo) {
        NewsClassifieds entity = typeDao.getOne(newsClassifiedsVo.getId());
        BeanUtils.copyProperties(newsClassifiedsVo,entity);
        typeDao.saveOrUpdate(entity);
    }

    /**
     * @Description 删除
     * @Param [ids]
     * @author ZhaoPing'an
     * @Date 2018/6/5 16:56
     */
    @Override
    public void deleteBatchIds(List<String> ids) {
        typeDao.deleteByIdIn(ids);
    }

    /**
     * @Description 获取所有资讯分类
     * @Param []
     * @author ZhaoPing'an
     * @Date 2018/6/8 9:34
     */
    @Override
    public List<NewsClassifiedsVo> findAll() {
        List<NewsClassifieds> list = typeDao.findAll();
        List<NewsClassifiedsVo> result = new ArrayList<>();
        for (NewsClassifieds temp:list) {
            NewsClassifiedsVo classifiedsVo = new NewsClassifiedsVo();
            BeanUtils.copyProperties(temp,classifiedsVo);
            result.add(classifiedsVo);
        }
        return result;
    }

}
